//
//  TestSqlGenerator.m
//  JhonSell
//
//  Created by Mario Montoya on 9/01/09.
//  Copyright 2009 El malabarista. All rights reserved.
//

#import <GHUnitIOS/GHUnit.h> 
#import "SqlGenerator.h"

@interface TestSqlGenerator : GHTestCase { }
@end

@implementation TestSqlGenerator

-(void) testSelect {
	//Simple select	
	NSString *sql = [SqlGenerator buildSelect:@"test" fields:nil filters:nil orders:nil];
	GHAssertEqualStrings(sql,@"SELECT * FROM [test]",@"Fail to build select");
	
	//With Field list
	NSArray *fields = [NSArray arrayWithObjects :@"Field1",@"Field2",nil];
	sql = [SqlGenerator buildSelect:@"test" fields:fields filters:nil orders:nil];
	GHAssertEqualStrings(sql,@"SELECT Field1, Field2 FROM [test]",@"Fail to build select with fields");

	//With order list
	NSArray *orderList = [NSArray arrayWithObjects :@"Field1",@"Field2",nil];
	sql = [SqlGenerator buildSelect:@"test" fields:fields filters:nil orders:orderList];
	GHAssertEqualStrings(sql,@"SELECT Field1, Field2 FROM [test] ORDER BY Field1, Field2",@"Fail to build select with order by");	

	//With where list
	NSArray *whereList = [NSArray arrayWithObjects :@"Field1=1",@"Field2='1'",nil];
	sql = [SqlGenerator buildSelect:@"test" fields:fields filters:whereList orders:nil];
	GHAssertEqualStrings(sql,@"SELECT Field1, Field2 FROM [test] WHERE (Field1=1) AND (Field2='1')",@"Fail to build select with where");
}

-(void) testDelete {
	//Full delete
	NSString *sql = [SqlGenerator buildDelete:@"test" filters:nil];
	GHAssertEqualStrings(sql,@"DELETE FROM [test]",@"Fail to build delete");
	
	//Delete with filter
	NSArray *whereList = [NSArray arrayWithObjects :@"Field1=1",@"Field2='1'",nil];
	sql = [SqlGenerator buildDelete:@"test" filters:whereList];
	GHAssertEqualStrings(sql,@"DELETE FROM [test] WHERE (Field1=1) AND (Field2='1')",@"Fail to build delete with where");
}

-(void) testUpdate {
	//Full delete
	NSDictionary * fieldAndValues = [NSDictionary dictionaryWithObjectsAndKeys:
									 [NSNumber numberWithInt:1], @"Field1",
									 @"1", @"Field2",
									 [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"], @"Field3",
									 nil];
	
	NSString *sql = [SqlGenerator buildInsertUpdate:@"test" fieldsAndValues: fieldAndValues];
	
	GHAssertEqualStrings(sql,@"INSERT OR REPLACE INTO [test] (Field1, Field3, Field2) VALUES (1, '2009-12-09T19:00:00', '1')",@"Fail to build update");
}

@end
